﻿// Decompiled with JetBrains decompiler
// Type: MCM.Implementation.PerCampaign.FluentPerCampaignSettingsContainer
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using BUTR.DependencyInjection;
using MCM.Abstractions;
using MCM.Abstractions.Base;
using MCM.Abstractions.Base.PerCampaign;
using MCM.Abstractions.GameFeatures;
using MCM.Abstractions.PerCampaign;
using System;
using System.Collections.Generic;
using System.Linq;

#nullable enable
namespace MCM.Implementation.PerCampaign
{
  internal sealed class FluentPerCampaignSettingsContainer : 
    BaseSettingsContainer<FluentPerCampaignSettings>,
    IFluentPerCampaignSettingsContainer,
    IPerCampaignSettingsContainer,
    ISettingsContainer,
    ISettingsContainerHasSettingsDefinitions,
    ISettingsContainerCanOverride,
    ISettingsContainerCanReset,
    ISettingsContainerPresets,
    ISettingsContainerHasUnavailable,
    ISettingsContainerHasSettingsPack,
    ISettingsContainerCanInvalidateCache
  {
    private readonly IGameEventListener _gameEventListener;

    public event Action? InstanceCacheInvalidated;

    protected override GameDirectory RootFolder { get; }

    public FluentPerCampaignSettingsContainer(IGameEventListener gameEventListener)
    {
      this._gameEventListener = gameEventListener;
      this._gameEventListener.GameStarted += new Action(this.GameStarted);
      this._gameEventListener.GameEnded += new Action(this.GameEnded);
      this.RootFolder = GenericServiceProvider.GetService<IFileSystemProvider>().GetDirectory(base.RootFolder, "PerCampaign");
    }

    protected override void RegisterSettings(FluentPerCampaignSettings? settings)
    {
      if (settings == null || GenericServiceProvider.GameScopeServiceProvider == null)
        return;
      ICampaignIdProvider service1 = GenericServiceProvider.GetService<ICampaignIdProvider>();
      string currentCampaignId;
      int num;
      if (service1 != null)
      {
        currentCampaignId = service1.GetCurrentCampaignId();
        num = currentCampaignId == null ? 1 : 0;
      }
      else
        num = 1;
      if (num != 0)
        return;
      IFileSystemProvider service2 = GenericServiceProvider.GetService<IFileSystemProvider>();
      if (service2 == null)
        return;
      this.LoadedSettings.Add(settings.Id, settings);
      GameDirectory directory1 = service2.GetOrCreateDirectory(this.RootFolder, currentCampaignId);
      GameDirectory directory2 = service2.GetOrCreateDirectory(directory1, settings.FolderName);
      GameDirectory directory3 = string.IsNullOrEmpty(settings.SubFolder) ? directory2 : service2.GetOrCreateDirectory(directory2, settings.SubFolder);
      ((IEnumerable<ISettingsFormat>) ((object) GenericServiceProvider.GetService<IEnumerable<ISettingsFormat>>() ?? (object) Array.Empty<ISettingsFormat>())).FirstOrDefault<ISettingsFormat>((Func<ISettingsFormat, bool>) (x => x.FormatTypes.Any<string>((Func<string, bool>) (y => y == settings.FormatType))))?.Load((BaseSettings) settings, directory3, settings.Id);
      settings.OnPropertyChanged("LOADING_COMPLETE");
    }

    public override bool SaveSettings(BaseSettings settings)
    {
      PerCampaignSettings perCampaignSettings = settings as PerCampaignSettings;
      if (perCampaignSettings == null || GenericServiceProvider.GameScopeServiceProvider == null)
        return false;
      ICampaignIdProvider service1 = GenericServiceProvider.GetService<ICampaignIdProvider>();
      string currentCampaignId;
      int num;
      if (service1 != null)
      {
        currentCampaignId = service1.GetCurrentCampaignId();
        num = currentCampaignId == null ? 1 : 0;
      }
      else
        num = 1;
      if (num != 0)
        return false;
      IFileSystemProvider service2 = GenericServiceProvider.GetService<IFileSystemProvider>();
      if (service2 == null)
        return false;
      GameDirectory directory1 = service2.GetOrCreateDirectory(this.RootFolder, currentCampaignId);
      GameDirectory directory2 = service2.GetOrCreateDirectory(directory1, settings.FolderName);
      GameDirectory directory3 = string.IsNullOrEmpty(settings.SubFolder) ? directory2 : service2.GetOrCreateDirectory(directory2, settings.SubFolder);
      ((IEnumerable<ISettingsFormat>) ((object) GenericServiceProvider.GetService<IEnumerable<ISettingsFormat>>() ?? (object) Array.Empty<ISettingsFormat>())).FirstOrDefault<ISettingsFormat>((Func<ISettingsFormat, bool>) (x => x.FormatTypes.Any<string>((Func<string, bool>) (y => y == perCampaignSettings.FormatType))))?.Save((BaseSettings) perCampaignSettings, directory3, perCampaignSettings.Id);
      settings.OnPropertyChanged("SAVE_TRIGGERED");
      return true;
    }

    public void Register(FluentPerCampaignSettings settings) => this.RegisterSettings(settings);

    public void Unregister(FluentPerCampaignSettings settings)
    {
      if (!this.LoadedSettings.ContainsKey(settings.Id))
        return;
      this.LoadedSettings.Remove(settings.Id);
    }

    private void GameStarted() => this.LoadedSettings.Clear();

    private void GameEnded() => this.LoadedSettings.Clear();

    public IEnumerable<UnavailableSetting> GetUnavailableSettings()
    {
      return (IEnumerable<UnavailableSetting>) Array.Empty<UnavailableSetting>();
    }
  }
}
